import React, {Component} from 'react';
import './index.less';
import {base} from 'nc-lightapp-front';
import moment from 'moment';
import EmptyImg from "../../../../../../hrpub/common/components/emptyImg";
import {chectFormAction} from "../../../../../public/functions/checkFormAction";

class EditForm extends Component {
    constructor(props) {
        super(props);
        this.state = {
            tableHeight: ''
        };
        this.setTableHeight = this.setTableHeight.bind(this);
        this.beforeEventHandle = this.beforeEventHandle.bind(this);
        this.afterEventHandle = this.afterEventHandle.bind(this);
    }

    componentDidMount() {
        this.setTableHeight();
    }

    //设置表的编辑状态
    setMainFormEdit(){

    }

    setTableHeight() {
        const mainTable = document.getElementsByClassName('main-table');
        if (mainTable[0]) {
            this.setState({
                tableHeight: mainTable[0].clientHeight - 36
            })
        }
    }

    beforeEventHandle(props, moduleId, item, index, value, record) {
        return true;
    }

    afterEventHandle(props, moduleId, key, value, changedrows, index, record) {

        chectFormAction(key, moduleId, props, [moduleId]);
        // if (key === 'termtype' && value === 'CM02') {
        //     record.values.itermmonth.value = "";
        //     record.values.sedenddate.value = "";
        //     this.props.editTable.setRowByIndex('protocolmgtchange', record, index);
        // }
        // if (key === "itermmonth") {
        //     const nextDate = this.getNextDate(value, record.values['pk_psndoc_sub.enddate'].value, record.values['contopinion_unit'].value)
        //     record.values.sedenddate.value = nextDate;
        //     this.props.editTable.setRowByIndex('protocolmgtchange', record, index);
        // }
    }

    getNextDate(itermmonth = 0, enddate, unit) {
        //"1":"年" "2":"月" "3": "天"
        let nextDate = "";
        if (unit === "1") {
            //nextDate = new Date(enddate).setFullYear(new Date(enddate).getFullYear() + Number(itermmonth));
            nextDate = moment(enddate).add(Number(itermmonth), 'y');
        }
        if (unit === "2") {
            //nextDate = new Date(enddate).setMonth(new Date(enddate).getMonth() + Number(itermmonth));
            nextDate = moment(enddate).add(Number(itermmonth), 'M');
        }
        if (unit === "3") {
            //nextDate = new Date(enddate).setDate(new Date(enddate).getDate() + Number(itermmonth));
            nextDate = moment(enddate).add(Number(itermmonth), 'd');
        }
        return nextDate.format("YYYY-MM-DD");
    }

    setDataBeforeFormKey (props, paramObj) {

    }

     //设置表的编辑状态
     setMainFormEdit(status) {
         const {agreementType} = this.props;
        /*设置表单编辑状态*/
        this.props.form.setFormStatus(agreementType+"Card", status);
    }
    render() {
        const {form, orgVal, agreementType} = this.props;
        return (
            <div className='main-table'>
                {form.createForm(agreementType+"Card", {
                    showIndex: true,
                    showCheck: true,
                    height: this.state.tableHeight,
                    onBeforeEvent: this.beforeEventHandle,
                    onAfterEvent: this.afterEventHandle
                })}
            </div>
        );
    }
}

export default EditForm;
